package com.zhoutao.blog.seven.config;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author: stopping
 * @Date: 2020/04/30 18:30
 * 转载注明出处、个人博客网站:www.stopping.top
 */
@Slf4j
public class PostUrl {

    @SneakyThrows
    public static void baiduPost(String sitePath, String url) {
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            //建立连接
            URLConnection connection = new URL(sitePath).openConnection();
            //设置请求属性
            connection.setRequestProperty("Host", "data.zz.baidu.com ");
            connection.setRequestProperty("User-Agent", "curl/7.12.1 ");
            connection.setRequestProperty("Content-Length", "83");
            connection.setRequestProperty("Content-Type", "text/plain");

            connection.setDoInput(true);
            connection.setDoOutput(true);
            //获取connection输出流
            out = new PrintWriter(connection.getOutputStream());
            out.print(url.trim());
            out.flush();


            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder result = new StringBuilder();
            while ((line = in.readLine()) != null) {
                result.append(line);
            }

        } catch (Exception e) {
            log.error("发送POST异常:" + url, e);
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}
